<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="stylesheet" href="/layui/css/layui.css" media="all">
</head>
<style type="text/css">
    .layui-form-item,.layui-input-inline,#btn{
        float: left;
        margin-left: 20%;
        margin-bottom: 15px;
        display: inline;
    }
    #btn{
        padding-left: 90px;
    }
    h2{
        padding-top: 40px;
        margin-bottom: 23px;
        color: #1E9FFF;
    }
    button{
        width: 140px;
        margin-bottom: 40px;
    }
    #div_one{
        border: 1px solid black;
        float: left;
        margin-left: 30%;
        margin-top: 170px;
    }
</style>
<body>
<div id="div_one">
    <h2 align="center">用户登录</h2>
    <form class="layui-form layui-form-pane" method="post">
        <div class="layui-form-item">
            <label class="layui-form-label">手机号</label>
            <div class="layui-input-inline">
                <input type="text" name="phone" lay-verify="required" placeholder="请输入手机号" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">验证码</label>
            <div class="layui-input-inline">
                <input type="text" name="verifyCode" placeholder="请输入验证码"class="layui-input">
            </div>
            <span id="smsbtn">获取验证码</span>
        </div>

        <div id="btn">
            <button type="submit" lay-submit lay-filter="formDemo"
                    class="layui-btn layui-btn-normal layui-btn-radius">登录</button>
            <b>还没有账号?<a href="/save.html" id="register">去注册!</a></b>
        </div>
    </form>
</div>
<script src="/layui/layui.js"></script>
<script>

    layui.use(['layer', 'table', 'jquery', 'form'], function(){
        var layer = layui.layer //弹层
            ,table = layui.table //表格
            ,$ = layui.jquery
            ,form = layui.form

        //监听提交
        form.on('submit(formDemo)', function(data){
            $.ajax({
                type:"post",
                url:"/user/login",
                data:{
                    "phone":data.field.phone,
                    "verifyCode":data.field.verifyCode,
                },
                dataType:"json",
                success:function(msg){
                    if (msg.code==0){
                        $("#resetBtn").click();
                        window.location.href="/index.html?username="+msg.data.username;
                    }else {
                        alert(msg.msg);

                    }
                }
            })
            return false;
        });
//发送短信
        var flag = true;
        $('span').click(function(){
            var phone = $("input[name=phone]").val();
            if($('input').val()==''){
                alert('请输入手机号');
            }else {
                if (phone.length < 11){
                    alert("请输入正确的手机号..")
                    return ;
                }else {
                    if(flag){
                        $.ajax({
                            type:"post",
                            url:"user/sendSms",
                            data:{
                                "phone":$("input[name=phone]").val()
                            },
                            dataType:"json",
                            success:function(msg){
                                alert("发送成功");
                            }
                        });
                        timer();
                    }
                }
            }
        });


        //前台的60秒倒计时
        function timer(){
            flag = false;
            var t = 60;
            $('span').html('60秒');
            var timer = setInterval(function(){
                t--;
                $('span').html(t+'秒');
                if(t == 0){
                    clearInterval(timer);
                    flag = true;
                    $('span').html('重新获取')
                }
            },1000)
        }
    });
</script>
</body>
</html>